<template>
  <div>
    <!-- //页面工具条的表单 -->
    <el-form ref="form" :model="searchform" :inline="true">

      <el-form-item>
        <el-input v-model="searchform.username" placeholder="请输入要搜索的内容"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button style="margin-right:2px">搜索</el-button>
        <el-button type="primary" style="margin-right:3px" @click="dialogFormVisible = true">新建</el-button>
        <!-- //气泡框 -->
        <el-popconfirm title="确认要批量删除商品信息吗？" @confirm="deleteProduct">
          <el-button slot="reference" type="danger" :disabled="deleteAllBtnStu">批量删除</el-button>
        </el-popconfirm>
      </el-form-item>
    </el-form>

    <!-- //data表格要显示的数据 -->
    <el-table :data="tableData" border scripe style="width:100%;margin-bottom: 10px;" :cell-style="{ padding: '2px' }"
              @selection-change="selectionChange">
      <el-table-column type="selection" width="50">
      </el-table-column>
      <el-table-column label="商品名称" width="150" prop="name">
      </el-table-column>
      <el-table-column label="商品图片" width="180" prop="avatar">
        <!-- 头像    scope代表循环中的每一行 -->
        <template slot-scope="scope">
          <el-image style="height: 100px; " :src="scope.row.avatar"></el-image>
        </template>
      </el-table-column>
      <el-table-column label="商品编码" width="100" prop="code">
      </el-table-column>
      <el-table-column label="描述" prop="remark">
      </el-table-column>
      <el-table-column label="库存" width="80" prop="sum">
      </el-table-column>

      <el-table-column label="状态" width="70" prop="statu">
        <template slot-scope="scope">
          <!-- <el-tag size="mini" type="success">正常</el-tag> -->
          <!-- if(scope.row.statu) -->
          <el-tag size="mini" v-if="scope.row.statu === 1" type="success">在售</el-tag>
          <el-tag size="mini" v-else-if="scope.row.statu === 0" type="info">售空</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="操作" width="160">
        <!-- prop对应列内容的字段名 -->
        <template slot-scope="scope">

          <el-button type="text" @click="editProduct(scope.row.id)">编辑</el-button>
          <el-divider direction="vertical"></el-divider>
          <el-popconfirm title="确认要删除该商品信息吗？" @confirm="deleteProduct(scope.row.id)">
            <el-button slot="reference" type="text">删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!-- 对话框 -->
    <el-dialog title="商品信息" :visible.sync="dialogFormVisible" width="600px" :before-close="handleClose">
      <el-form :model="ProductForm" ref="ProductForm" label-width="100px" :rules="rules">
        <el-form-item label="商品照片" label-width="100px">
          <el-upload class="avatar-uploader" action="/api/upload" :show-file-list="false"
                     :on-success="handleAvatarSuccess" :headers="headers">
            <!-- :headers="headers" -->
            <!-- //上传成功会获得路径回显 -->
            <img v-if="ProductForm.avatar" :src="ProductForm.avatar" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="商品名称" label-width="100px" prop="name">
          <el-input v-model="ProductForm.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="商品编号" label-width="100px" prop="code">
          <el-input v-model="ProductForm.code" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="库存数量" label-width="100px" prop="sum">
          <el-input v-model="ProductForm.sum" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="描述" label-width="100px" prop="remark">
          <el-input v-model="ProductForm.remark" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="状态" label-width="100px" prop="statu">
          <el-radio-group v-model="ProductForm.statu">
            <el-radio :label="1">在售</el-radio>
            <el-radio :label="0">售空</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ProductForm')">保存商品</el-button>
          <el-button @click="resetForm('ProductForm')">重置</el-button>
        </el-form-item>

      </el-form>
    </el-dialog>
    <!-- 显示分页功能 -->
    <el-pagination layout="prev, sizes,pager, next, total, jumper " :total="total" :page-size="size"
                   :page-sizes="[5,10,15,20]" :current-page="current" style="margin-top: 20px">
    </el-pagination>
  </div>
</template>

<script>
export default {
  data() {
    return {
      searchform: {
        username: '',
      },
      dialogFormVisible: false,
      deleteAllBtnStu: true,  //批量删除按钮
      tableData: [],
      selection: [],
      ProductForm: {},
      total: 0,
      size: 10,//一页显示的记录数
      current: 1,//当前显示的页码
      //搜索栏
      headers: {
        token: localStorage.getItem('token')
      },
      rules:
          {
            name: [{required: true, message: '请输入商品名称！', trigger: 'blur'}],
            code: [{required: true, message: '请输入商品编号！', trigger: 'blur'}],
            sum: [{required: true, message: '请输入商品库存！', trigger: 'blur'}],
            statu: [{required: true, message: '请选择商品状态！', trigger: 'blur'}],
            remark: [{required: true, message: '请输入商品描述！', trigger: 'blur'}],
          },
    }
  },
  created() {
    this.loadProductList();
  },
  methods: {
    handleClose() {
      //关闭新建用户表单
      this.ProductForm = {}
      this.dialogFormVisible = false
    },
    handleAvatarSuccess(response, file) {
      //上传头像成功。，服务器返回一个上传后的图片网络地址
      this.ProductForm.avatar = response.data
    },
    loadProductList() {
      this.$axios.get('/system/product/list',
          {
            current: this.current,
            size: this.size
          }).then(response => {
        this.tableData = response.data.data.records
        this.size = response.data.data.size
        this.total = response.data.data.total
        this.current = response.data.data.current
      }).catch(error => {
        console.log(error)
      })
    },
    deleteProduct(id) {
      let ids = [];//ids数组要删除的所有用户数据
      id ? ids.push(id) : this.selection.forEach(u => {
        ids.push(u.id)
      })
      this.$axios.post('/system/product/delete', ids).then(response => {
        this.$message({
          message: response.data.message,
          type: 'success',
          showClose: true,
          onClose: () => {
            this.loadCategoryList()
          }
        })
      })
    },
    editProduct(id) {
      this.$axios('/system/product/info/' + id).then(response => {
        this.ProductForm = response.data.data
        this.dialogFormVisible = true
      })
    },
    selectionChange(value) {
      this.selection = value
      if (this.selection) {
        this.deleteAllBtnStu = (this.selection.length == 0)
      }
    },
    submitForm(formName) {
      this.$refs.ProductForm.validate((valid) => {
        if (valid) {
          this.$axios.post('/system/product/' + (this.ProductForm.id ? 'update' : 'save')).then(response => {
            this.$message({
              message: response.data.message,
              type: 'success',
              // showClose: true,
              duration: 1500,//1.5秒后自动关闭对话框
              onClose: () => {
                this.loadProductList()  //需要重新加载用户信息
                this.dialogFormVisible = false //对话框是否显示
                this.resetForm(formName)  //重置表单
              }
            })

          })
        }
      })
    },//重置表单
    resetForm(formName) {
      this.ProductForm = {}
      this.$refs.ProductForm.resetFields();
    },
  },
  components: {}
}
</script>

<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #9b9a9a;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.el-table__body {
  font-size: 14px;
}

.el-button:hover {
  color: #333
}

.el-popconfirm-reference {
  cursor: pointer;
}

.el-pagination {
  display: flex;
  justify-content: flex-end;
}

.el-tag {
  font-size: 12px;
  padding: 2px 6px;
}

.el-table-column--selection {
  text-align: center;
}

.el-avatar {
  border-radius: 50%;
}

.el-divider {
  margin: 0 6px;
}

/*上传头像*/
/*.avatar-uploader {*/
/*  border: 1px dashed #d9d9d9;*/
/*  border-radius: 6px;*/
/*  cursor: pointer;*/
/*  position: relative;*/
/*  overflow: hidden;*/
/*}*/

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

.el-dialog {
  text-align: center;
}

/* Style the dialog header */
.el-dialog__header {
  background-color: #2f4050;
  color: #fff;
  border-radius: 4px 4px 0 0;
  border-bottom: none;
}

/* Style the dialog title */
.el-dialog__title {
  font-size: 22px;
  font-weight: bold;
  padding: 20px;
}

/* Style the form labels */
.el-form-item__label {
  font-weight: bold;
  font-size: 16px;
  color: #333;
}

/* Style the input fields */
.el-input__inner {
  border-radius: 4px;
  border: 1px solid #dcdfe6;
  color: #606266;
}

/* Style the radio buttons */


.el-radio-button {
  flex: 1;
}

.dialog-footer {
  padding: 20px;
  text-align: right;
  border-top: 1px solid #ebeef5;
  border-radius: 0 0 4px 4px;
}

.dialog-footer .el-button {
  margin-left: 10px;
  border-radius: 4px;
}

.dialog-footer .el-button--primary {
  background-color: #409EFF;
  border-color: #409EFF;
}

.dialog-footer .el-button--primary:hover {
  background-color: #66b1ff;
  border-color: #66b1ff;
}

.dialog-footer .el-button--primary:active {
  background-color: #3a8ee6;
  border-color: #3a8ee6;
}

.dialog-footer .el-button--default {
  color: #333;
  border-color: #dcdfe6;
}

.dialog-footer .el-button--default:hover {
  color: #409EFF;
  border-color: #409EFF;
}
</style>
